//
//  GridScrollView.m
//  FootPrint
//
//  Created by alex tao on 3/21/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "GridScrollView.h"

@implementation GridScrollView

- (void)internalInit
{
    _cells = [[NSMutableArray alloc] initWithCapacity:4];
    _colCnt = 4;
    
    UITapGestureRecognizer * tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)] autorelease];
    [self addGestureRecognizer:tapGesture];
    
    self.clipsToBounds = YES;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [self internalInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self internalInit];
    }
    return self;
}

- (void)dealloc
{
    _gridDelegate = nil;
    [_cells release];
    [super dealloc];
}

- (void)handleTap:(UITapGestureRecognizer*)tap
{
    CGPoint pos = [tap locationInView:self];
    
    if (_colCnt == 0) {
        _colCnt = 1;
    }
    
    CGSize allSz = self.bounds.size;
    
    CGFloat cellWidth = allSz.width/_colCnt;
    if (_cellHeight == 0) {
        _cellHeight = cellWidth;
    }
    
    NSInteger rowIdx = pos.y/_cellHeight;
    NSInteger colIdx = pos.x/cellWidth;
    
    NSInteger idx = rowIdx*_colCnt+colIdx;
    if (idx < _cells.count) {
        [_gridDelegate didSelect:self atTotalIndex:idx];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) resetAll
{
    for (UIView * view in _cells) {
        [view removeFromSuperview];
    }
    [_cells removeAllObjects];
}

- (void)setCells:(NSMutableArray *)cells
{
    [self resetAll];
    [_cells release];
    _cells = [cells retain];
    
    [self setNeedsLayout];
}


- (void)layoutSubviews
{
    if (_colCnt == 0) {
        _colCnt = 1;
    }
    
    CGSize allSz = self.bounds.size;
    
    CGFloat cellWidth = allSz.width/_colCnt;
    if (_cellHeight == 0) {
        _cellHeight = cellWidth;
    }
    
    if (_oneRowMode) {
        [_cells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:[UIView class]]) {
                UIView * view = (UIView*)obj;
                view.center = CGPointMake(idx*cellWidth+cellWidth/2, _cellHeight/2);
                [self addSubview:view];
            }
        }];
        self.contentSize = CGSizeMake(_cells.count*cellWidth, _cellHeight);
    } else {
        [_cells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:[UIView class]]) {
                UIView * view = (UIView*)obj;
                view.center = CGPointMake((idx%_colCnt)*cellWidth+cellWidth/2, (idx/_colCnt)*_cellHeight+_cellHeight/2);
                [self addSubview:view];
            }
        }];
        self.contentSize = CGSizeMake(allSz.width, ceilf(_cells.count/(float)_colCnt)*_cellHeight);
    }
}

@end
